$(function () {
    var memberInfo_dig = $("#memberInfo_dig");
    var memberInfo_dg = $('#memberInfo_dg');
    var memberInfo_form = $('#memberInfo_form');
    var charge_datagrid = $('#charge_datagrid');
    var gift_datagrid = $('#gift_datagrid');
    var dig_charge = $('#dig_charge');
    var dig_gift = $('#dig_gift');
    /*给所有的a标签绑定点击事件*/

    var ObjectCmd ={
        /*保存*/
        saveUser:function () {


            memberInfo_form.form("clear")
            $("#password").show();
            memberInfo_dig.dialog("setTitle", "add");
            memberInfo_dig.dialog("open");
            
        },
        //导出Excel链接请求
        exportData:function () {
          window.location.href = "/memberInfo/exportData.do";
        },
        /*编辑*/
        editUser: function editUser() {
            var tr = memberInfo_dg.datagrid("getSelected");

            if (!tr) {
                //没有选中
                $.messager.alert('温馨提示', "至少选中一条数据");
                return;
            }

            memberInfo_form.form("clear")
            //隐藏
            $("#password").hide()
            //用户名只读
            memberInfo_dig.dialog("setTitle", "editUser");
            memberInfo_dig.dialog("open");
            memberInfo_form.form("load", tr)
        },
        /*离职*/
        changeUser: function () {
            var tr = memberInfo_dg.datagrid("getSelected");
            if (!tr) {
                //没有选中
                $.messager.alert('温馨提示', "至少选中一条数据");
                return;
            }
            $.get("/memberInfo/changeState.do", {id: tr.id}, function (data) {
                if (data.success) {
                    $.messager.alert('温馨提示', '操作成功', 'info', function () {


                        memberInfo_dg.datagrid("reload");
                    });
                }
                else {
                    $.messager.alert('温馨提示', data.msg);


                }
            })
        },
        /*刷新*/
        reloadwin: function () {
            memberInfo_dg.datagrid("reload");
        },
        /*高级查询*/
        queryForKey: function () {
            var keyword=  $("input[name='keyword']").val();
            memberInfo_dg.datagrid("load",{"keyword":keyword});
        },
        /*关闭对话框*/
        closeWin:function () {
            memberInfo_dig.dialog("close")

        },
        /*提交表单*/
        submitForm:function () {
            /*提交角色*/
            memberInfo_form.form({
                url: '/memberInfo/saveOrUpdate.do',

                success: function (data) {
                    var data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert('温馨提示', '保存成功', 'info', function () {

                            ObjectCmd.closeWin();
                            memberInfo_dg.datagrid("reload");
                        });
                    }
                    else {
                        $.messager.alert('温馨提示', data.msg);
                        ObjectCmd.closeWin();

                    }
                }
            });


            memberInfo_form.submit();
        },
        /*查询指定会员的充值记录*/
        queryCharge:function () {
            var tr = memberInfo_dg.datagrid("getSelected");
            if (!tr) {
                //没有选中
                $.messager.alert('温馨提示', "至少选中一条数据");
                return;
            }
            $.get("/charge/queryByCard.do", {cardNumber: tr.cardnumber}, function (data) {
                if (data) {
                    charge_datagrid.datagrid({
                        data:data,
                        fit:true,
                        fitColumns:true,
                        striped:true,

                        columns:[
                            [
                                {field:'cardNumber',title:'卡号',align:'center',width:100},
                                {field:'operateWay',title:'操作方式',width:100,align:'center',formatter:function (value) {
                                        return value ? '充值':'扣费'}},
                                {field:'payType',title:'支付类型',align:'center',width:100,formatter:function (payType) {
                                        if(payType==0){
                                            return '现金';
                                        }else if(payType==1){
                                            return '现金';
                                        }else if (payType==2){
                                            return '支付宝';
                                        }else if(payType==3){
                                            return '微信';
                                        }
                                    }},
                                {field:'oldBalance',title:'变动前金额',align:'center',width:100},
                                {field:'chargeAmount',title:'充值金额',align:'center',width:100},
                                {field:'newBalance',title:'变动后金额',align:'center',width:100},
                                {field:'chargeDate',title:'充值日期',align:'center',width:100},

                                {field:'username',title:'操作人',align:'center',width:100},
                                {field:'remark',title:'充值备注',align:'center',width:100},
                            ]
                        ],
                        pagination:true,
                        rownumbers:true,
                        singleSelect:true,
                    });
                    dig_charge.dialog("open")
                }
                else {
                    $.messager.alert('温馨提示', data.msg);


                }
            })
            
        },
    /*查询会员兑换记录*/
        queryGift:function () {
            var tr = memberInfo_dg.datagrid("getSelected");
            if (!tr) {
                //没有选中
                $.messager.alert('温馨提示', "至少选中一条数据");
                return;
            }
            $.get("/giftbill/queryByMemberId.do", {memberId: tr.id,page: 1,rows: 10,sort: 'inputTime',order: 'desc'}, function (data) {
                if (data) {
                    gift_datagrid.datagrid({
                        fit:true,
                        fitColumns:true,
                        data:data,

                        sortName: "inputTime",
                        sortOrder: "desc",
                        striped:true,
                        columns:[[
                            {field:'memberName',title:'会员名字',width:100},
                            {field:'giftName',title:'礼品名称',width:100},
                            {field:'number',title:'兑换数量',width:100,},
                            {field:'points',title:'兑换一个所需积分',width:100},
                            {field:'totalPoints',title:'兑换需要的积分',width:100},
                            {field:'inputId',title:'操作人ID',width:100},
                            {field:'inputName',title:'操作人名字',width:100},
                            {field:'inputTime',title:'兑换时间',width:100,sortable: true,order:"desc"},
                        ]],
                        toolbar:"#gift_tb",
                        singleSelect:true,
                        pagination:true,
                        rownumbers: true,
                    });
                    dig_gift.dialog("open")
                }
                else {
                    $.messager.alert('温馨提示', data.msg);


                }
            })

        },

        querySale: function () {
            var tr = memberInfo_dg.datagrid("getSelected");
            if (!tr) {
                //没有选中
                $.messager.alert('温馨提示', "至少选中一条数据");
                return;
            }
            $.get("/saleorder/memberPurchaseRecord.do", {
                memberId: tr.id,
                page: 1,
                rows: 10,
            }, function (data) {
                if (data) {
                    gift_datagrid.datagrid({
                        fit: true,
                        fitColumns: true,
                        striped: true,
                        columns: [[
                            {field: 'buyDate', title: '购买时间', width: 100},
                            {field: 'productName', title: '商品名称', width: 100},
                            {field: 'productSn', title: '商品编码', width: 100},
                            {field: 'buyPrice', title: '购买价格', width: 100,},
                            {field: 'buyNumber', title: '购买数量', width: 100},
                            {field: 'buyAmount', title: '购买总价', width: 100},
                            {field: 'buyRemark', title: '备注', width: 100},
                        ]],
                        toolbar: "#gift_tb",
                        singleSelect: true,
                        pagination: true,
                        rownumbers: true,
                    });
                    gift_datagrid.datagrid("loadData", data);
                    dig_gift.dialog("open")
                }
                else {
                    $.messager.alert('温馨提示', data.msg);


                }
            })

        },

    }
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd")
        ObjectCmd[cmd]();
    })





    //编辑对话框
    memberInfo_dig.dialog({
        width: 400,
        height: 400,
        closed: true,
        cache: false,
        modal: true,
        buttons: '#memberInfo_dig_btn'
    })
    //页面table
    memberInfo_dg.datagrid({
        url: '/memberInfo/query.do',
        fitColumns: true,
        fit: true,
        pagination: true,
        singleSelect: true,
        toolbar: "#memberInfo_tb",
        rownumbers: true,
        onClickRow: function (index, row) {

            if (row.state) {

                $("#btn_change").linkbutton({iconCls: 'icon-no', text: '禁用'})
            }
            else {
                $("#btn_change").linkbutton({iconCls: 'icon-ok', text: '启用'})

            }
        },
        columns: [[
            {field:'name',title:'会员姓名',width:100,align:'center'},
            {field:'cardnumber',title:'卡号',width:100,align:'center'},
            {field:'tel',title:'电话号码',width:100,align:'center'},
            {field:'rankId',title:'会员等级',width:100,align:'center'},
            {field:'points',title:'可用积分',width:100,align:'center'},
            {field:'balance',title:'存储余额',width:100,align:'center'},
            {field:'gender',title:'性别',width:100,align:'center',formatter:function (value) {
                    return value ? '女':'男';
                }},
            {field:'address',title:'地址',width:100,align:'center'},
            {field:'consumption',title:'消费金额',width:100,align:'center'},
            {field:'consumeNumber',title:'充值次数',align:'center',width:100},
            {field:'chargeTotalAmount',title:'充值总金额',align:'center',width:100},
            {field:'date',title:'开卡时间',width:100,align:'center'},
            {field:'memberbirthday',title:'会员生日',width:100,align:'center'},
            {
                field: 'state', title: '状态', width: 100, formatter: function (value, row, index) {
                    if (value) {
                        return "正常";
                    } else {
                        return "<span style='color: red'>禁用</span>";
                    }
                }
            },

        ]]
    });
})




